package com.lancoo.tyjx.multichatroom.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.lancoo.tyjx.multichatroom.xmpp.XmppMsgType;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class JsonUtil {
    public static String HEADER_IMG = "";

    /**
     * 获取assert目录下的表情json
     *
     * @param fileName
     * @param context
     * @return
     */
    public static String getEmotionJson(String fileName, Context context) {
        // 将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // 获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            // 通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 解析表情json
     *
     * @param emotionJson
     * @return
     */
    public static HashMap<String, String> parseEmotionJson(String emotionJson) {
        HashMap<String, String> emotionMap = new HashMap<String, String>();
        try {
            JSONArray emotionList = new JSONObject(emotionJson)
                    .getJSONArray("emotion_list");
            for (int i = 0; i < emotionList.length(); i++) {
                JSONObject jsonObj = (JSONObject) emotionList.get(i);
                String toolTip = "[" + jsonObj.getString("tooltip") + "]";
                String emotionName = jsonObj.getString("file_name");
                emotionMap.put(toolTip, emotionName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return emotionMap;
    }

    public static String objectToJson(Object object) {
        Gson gson = new Gson();
        String json = gson.toJson(object);
        return json;
    }

    /**
     * 封装json
     *
     * @return
     */
    public static String xmppMsg2Json(XmppMsgType msgType, Object... parms) {
        String json = "";
        JSONObject jsonMain = new JSONObject();
        try {
            jsonMain.put("SendTime",DateUtil.getYY_MM_dd_ss());
            switch (msgType) {
                case NORMAL:
                    jsonMain.put("MessageType", "normal");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject jsonNormalBody = new JSONObject();
                    jsonNormalBody.put("Message", parms[0]);
                    jsonMain.put("MessageBody", jsonNormalBody);
//				json = StringUtil.utf82gbk(jsonMain.toString());
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.normal2Json()====" + json);
                    break;
                case LIKE:
                    jsonMain.put("MessageType", "like");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject jsonLikeBody = new JSONObject();
                    jsonLikeBody.put("Operation", parms[0]);
                    jsonMain.put("MessageBody", jsonLikeBody);
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.like2Json()====" + json);
                    break;
                case AUDIO:
                    jsonMain.put("MessageType", "audio");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject jsonAudioBody = new JSONObject();
                    jsonAudioBody.put("Base64", parms[1]);
                    jsonAudioBody.put("FileName", parms[0]);
                    jsonAudioBody.put("Duration", parms[2]);
                    jsonMain.put("MessageBody", jsonAudioBody);
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.audio2Json()====" + json);
                    break;
                case PICTURE:
                    jsonMain.put("MessageType", "picture");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject jsonPictureBody = new JSONObject();
                    jsonPictureBody.put("FileName", parms[0]);
                    jsonPictureBody.put("Base64", parms[1]);
                    jsonPictureBody.put("FileURL", parms[2]);
                    jsonMain.put("MessageBody", jsonPictureBody);
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.picture2Json()====" + json);
                    break;
                case ASK:
                    jsonMain.put("MessageType", "ask");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject askJsonBody = new JSONObject();
                    askJsonBody.put("Message", parms[0]);
                    if (null != parms[1]) {
                        JSONArray jsonArray = new JSONArray();
                        jsonArray.put(parms[1]);
                        askJsonBody.put("PicURL", jsonArray);
                    }
                    jsonMain.put("MessageBody", askJsonBody);
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.picture2Json()====" + json);
                    break;
                case FILE:
                    jsonMain.put("MessageType", "file");
                    jsonMain.put("HeaderImg", HEADER_IMG);
                    JSONObject fileBody = new JSONObject();
                    fileBody.put("FileName", parms[0]);
                    fileBody.put("FileURL", parms[1]);
                    jsonMain.put("MessageBody", fileBody);
                    json = jsonMain.toString();
                    System.out.println("JsonUtil.file2Json()====" + json);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static String whatMsgType(String json) {
        String type = "";
        try {
            JSONObject jsonMain = new JSONObject(json);
            type = jsonMain.getString("MessageType");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return type;
    }

    /**
     * 解析消息json
     *
     * @param json
     * @return
     */
    public static String json2XmppMsg(String json) {
        String xmppMsg = "";
        try {
            JSONObject jsonMain = new JSONObject(json);
            String type = jsonMain.getString("MessageType");
            JSONObject jsonBody = jsonMain.getJSONObject("MessageBody");
            switch (type) {
                case "normal":
                    xmppMsg = jsonBody.getString("Message");
                    break;
                case "like":
                    xmppMsg = jsonBody.getString("Operation");
                    break;
                case "audio":
                    xmppMsg = jsonBody.getString("FileName") + ":::" + jsonBody.getString("Base64");
                    break;
                case "picture":
                    xmppMsg = jsonBody.getString("FileName") + ":::" + jsonBody.getString("Base64") + ":::" + jsonBody.getString("FileURL");
                    break;
                case "file":
//                    xmppMsg = jsonBody.getString("FileName") + ":::" + jsonBody.getString("FileURL");
                    xmppMsg = "No supported";
                    break;
                case "notice":
                    xmppMsg = jsonBody.getString("Message");
                    break;
                default:
                    xmppMsg = "No supported";
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return xmppMsg;
    }

    /**
     * 解析消息json
     *
     * @param json
     * @return
     */
    public static String json2XmppMsgHeaderImg(String json) {
        JSONObject jsonMain = null;
        String audiotime = "";
        try {
            jsonMain = new JSONObject(json);
            audiotime = jsonMain.getString("HeaderImg");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return audiotime;
    }

    public static int getAudioTime(String json) {
        JSONObject jsonMain = null;
        String audiotime = "0";
        try {
            jsonMain = new JSONObject(json);
            audiotime = jsonMain.getJSONObject("MessageBody").getString("Duration");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return Integer.valueOf(audiotime);
    }

    public static String getFileName(String json) {
        JSONObject jsonMain = null;
        String fileName = "";
        try {
            jsonMain = new JSONObject(json);
            fileName = jsonMain.getJSONObject("MessageBody").getString("FileName");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public static String getFileUrl(String json) {
        JSONObject jsonMain = null;
        String fileUrl = "";
        try {
            jsonMain = new JSONObject(json);
            fileUrl = jsonMain.getJSONObject("MessageBody").getString("FileURL");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

    public static String getSendTime(String json) {
        JSONObject jsonMain = null;
        String audiotime = "";
        try {
            jsonMain = new JSONObject(json);
            audiotime = jsonMain.getString("SendTime");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return audiotime;
    }
}
